# -*- coding: utf-8 -*-
from flask import Flask, request
from models import Author
from models import AuthorZiliao
from models import Gushiwen
from . import work
from flask import jsonify
from templates import responsecode
import json

IMAGE_URL = 'http://118.25.228.94:8190/gushici/img/%s.jpg'
IMAGE_SUFFIX = '.jpg'


@work.route('/author/popular')
def author_popular():
    """
    分页获取诗人信息
    :return:
    """
    resp_result = {
        'code': responsecode.SUCCESS_CODE,
        'result': '',
        'msg': responsecode.SUCCESS_MSG
    }
    args = request.args
    try:
        p = int(args.get('p'))
        s = int(args.get('s'))
        print("pagename=", p)
        print("pagesize=", s)
    except:
        resp_result['code'] = responsecode.PARAMS_ERROR_CODE
        resp_result['msg'] = responsecode.PARAMS_ERROR_MSG
        return resp_result
    authors = Author.query.order_by(-Author.likes).limit(s).offset(s*(p-1)).all()
    if authors is not None and len(authors) > 0:
        list = []
        for author in authors:
            author_data = author.getResponseModel()
            pic = author.pic.strip()
            if len(pic) > 0:
                author_data['icon'] = pic + IMAGE_SUFFIX
            list.append(author_data)
        resp_result['result'] = list
    return jsonify(resp_result)


@work.route('/author/detail')
def author_detail():
    """
    获取或者信息
    :return:
    """
    resp_result = {
        'code': responsecode.SUCCESS_CODE,
        'result': '',
        'msg': responsecode.SUCCESS_MSG
    }
    args = request.args
    try:
        name = args.get('name').strip()
    except:
        resp_result['code'] = responsecode.PARAMS_ERROR_CODE
        resp_result['msg'] = responsecode.PARAMS_ERROR_MSG
        return resp_result
    author = Author.query.filter(Author.nameStr == name).first()
    result_data = {
        'author':'',
        'ziliao':[]
    }
    if author is not None:
        author_data = {
            'name': author.nameStr,
            'id': author.id,
            'authorid': author.idnew,
            'chaodai': author.chaodai,
            'count': author.shiCount
        }
        pic = author.pic.strip()
        if len(pic) > 0:
            author_data['icon'] = pic + IMAGE_SUFFIX
        result_data['author'] = author_data
        author_ziliaos = AuthorZiliao.query.filter(AuthorZiliao.authorID == author.id).all()
        if author_ziliaos is not None and len(author_ziliaos) > 0:
            list = []
            for value in author_ziliaos:
                list.append(value.getResponseModel())
            result_data['ziliao'] = list
    resp_result['result'] = result_data
    return jsonify(resp_result)


@work.route('/gsw/listbyauthor')
def gws_list_by_author():
    """
    分页获取诗人的作品信息
    :return:
    """
    resp_result = {
        'code': responsecode.SUCCESS_CODE,
        'result': '',
        'msg': responsecode.SUCCESS_MSG
    }
    args = request.args
    try:
        p = int(args.get('p'))
        s = int(args.get('s'))
        aname = args.get('aname').strip()
    except:
        resp_result['code'] = responsecode.PARAMS_ERROR_CODE
        resp_result['msg'] = responsecode.PARAMS_ERROR_MSG
        return resp_result
    gushiwens = Gushiwen.query.filter(Gushiwen.author == aname).order_by(-Gushiwen.exing).limit(s).offset(s*(p-1)).all()
    if gushiwens is not None and len(gushiwens) > 0:
        list = []
        for gushiwen in gushiwens:
            list.append(gushiwen.getResponseModel())
        resp_result['result'] = list
    return jsonify(resp_result)